#include "stdafx.h"
#include "TestTree.h"
#include "Treetest.h"
#include "PDMSPerObjManager.h"

CPPUNIT_TEST_SUITE_REGISTRATION( TestTree );


TestTree::TestTree()
{
}


TestTree::~TestTree()
{
}

void TestTree::TestAdd()
{
	UESoft::ErrorStatus es = UESoft::eOk;

	PDMS::PDMSFrame::PDMSPerObjManager manager;
	PDMS::PDMSFrame::Tree* pTree = NULL;
	es = manager.TreeModel( pTree );
	CPPUNIT_ASSERT( es == UESoft::eOk );

	PDMS::PDMSFrame::Tree* ptmpTree = new TreeTest;
	es = pTree->Add( ptmpTree );
	CPPUNIT_ASSERT( es == UESoft::eOk );
	CPPUNIT_ASSERT( pTree->SubTreeCount() == 1 );
	CPPUNIT_ASSERT( !ptmpTree->Id().IsNull() );

	es = pTree->Add( ptmpTree );
	CPPUNIT_ASSERT( es == UESoft::eAlreadyInDb );
	CPPUNIT_ASSERT( pTree->SubTreeCount() == 1 );
	CPPUNIT_ASSERT( manager.HashModel()->Size() == 2 );

	es = ptmpTree->Add( new TreeTest );
	CPPUNIT_ASSERT( es == UESoft::eOk );
	CPPUNIT_ASSERT( ptmpTree->SubTreeCount() == 1 );
	CPPUNIT_ASSERT( manager.HashModel()->Size() == 3 );
	ptmpTree->Close();

	ptmpTree = new TreeTest;
	ptmpTree->Id( UeObjectId( 10, 20 ) );
	es = pTree->Add( ptmpTree );
	CPPUNIT_ASSERT( es == UESoft::eOk );
	CPPUNIT_ASSERT( ptmpTree->Id() == UeObjectId( 10, 20 ) );
	pTree->Close();

}

void TestTree::TestRemoveAll()
{
	PDMS::PDMSFrame::PDMSPerObjManager manager;
	PDMS::PDMSFrame::Tree* pTree = NULL;
	UESoft::ErrorStatus es = manager.TreeModel( pTree );
	
	pTree->Add( new TreeTest );	
	CPPUNIT_ASSERT( pTree->SubTreeCount() == 1 );
	CPPUNIT_ASSERT( manager.HashModel()->Size() == 2 );

	pTree->RemoveAll();
	CPPUNIT_ASSERT( pTree->SubTreeCount() == 0 );
	pTree->Close();
}

void TestTree::TestIterator()
{
	PDMS::PDMSFrame::PDMSPerObjManager manager;
	PDMS::PDMSFrame::Tree* pTree = NULL;
	UESoft::ErrorStatus es = manager.TreeModel( pTree );
	CPPUNIT_ASSERT( es == UESoft::eOk );

	PDMS::PDMSFrame::Tree* ptmpTree = NULL;
	PDMS::PDMSFrame::Tree::iterator it;
	PDMS::PDMSFrame::Tree::iterator it1;
	CPPUNIT_ASSERT( it.end() );

	CPPUNIT_ASSERT( ptmpTree == NULL );

	it = pTree->Iterator();
	CPPUNIT_ASSERT( it.end() );
	CPPUNIT_ASSERT( ptmpTree == NULL );
	
	ptmpTree = new TreeTest;
	ptmpTree->Name( "Test" );
	pTree->Add( ptmpTree );	
	ptmpTree->Close();
	it = pTree->Iterator();
	pTree->Close();

	it1 = it;
	CPPUNIT_ASSERT( !it.end() );
	es = it.Get( ptmpTree );
	CPPUNIT_ASSERT( es == UESoft::eOk );
	CPPUNIT_ASSERT( ptmpTree != NULL );
	it++;
	CPPUNIT_ASSERT( it.end() );
	PDMS::PDMSFrame::Tree* ptmpTree1 = NULL;
	es = it.Get( ptmpTree1 );
	CPPUNIT_ASSERT( es == UESoft::eIteratorDone );
	CPPUNIT_ASSERT( ptmpTree1 == NULL );

	CPPUNIT_ASSERT( !it1.end() );
	es = it1.Get( ptmpTree1 );
	CPPUNIT_ASSERT( es == UESoft::eWasOpenForWrite );
	CPPUNIT_ASSERT( ptmpTree1 == NULL );

	ptmpTree->Close();

	es = it1.Get( ptmpTree1 );
	CPPUNIT_ASSERT( es == UESoft::eOk );
	CPPUNIT_ASSERT( ptmpTree1 != NULL );
	ptmpTree1->Close();
	it1++;
	CPPUNIT_ASSERT( it1.end() );
	es = it1.Get( ptmpTree1 );
	CPPUNIT_ASSERT( es == UESoft::eIteratorDone );
	CPPUNIT_ASSERT( ptmpTree1 == NULL );
}